import {
    ApprovalDeliveryReceipt,
    copyDeliveryReceipt,
    createDeliveryReceipt,
    deleteById,
    getById,
    getListForPage,
    getUserDeliveryReceipt,
    updateDeliveryReceipt
} from '../../services/logisticsOrder/deliveryReceipt';
import {message} from 'antd';
import Constants from '../../utils/Constants'

const R = require('ramda');

export default {
    namespace: 'deliveryReceipt',
    state:{
        page: {},
        list: [],
        dataSource: {},
        company: {},
        deliveryReceipt: {},
        flag: []    //存在数据则不可新增交接单
    },
    reducers: {
        /**
         * 刷新state
         * 
         * @param state 
         */
        refreshState(state, { payload }){
            return { ...state, ...payload };
        }
    },
    effects: {
        /**
         * 分页查询交接单
         * 
         * @param call
         * @param put
         * 
         */
        *getListForPage({ payload }, { call, put }){
            const res = yield call(getListForPage, { ...R.omit(['onSuccess', 'onFail'])(payload) });
            if(res && res.data.resultCode === "ok"){
                yield put({
                    type:'refreshState',
                    payload: {
                        list: res.data.content.list,
                        page: res.data.content.page
                    }
                });
            } else {
                if(payload.onFail) payload.onFail();
            }
        },
        /**
         * 根据id获取交接单
         * 
         * @param id
         */
        *getById({ payload }, { call, put }){
            if(payload.id){
                const res = yield call(getById, { ...R.omit(['onSuccess', 'onFail'])(payload) });
                if(res && res.data.resultCode === "ok"){
                    yield put({
                        type: 'refreshState',
                        payload: {
                            company: res.data.content.company ? res.data.content.company : {},
                            deliveryReceipt: res.data.content.deliveryReceipt ? res.data.content.deliveryReceipt : {}
                        }
                    });
                } else {
                    if(payload.onFail) payload.onFail();
                }
            } else {
                yield put({
					type: 'refreshState',
					payload: {
						dataSource: {}
					}
				});
            }
        },
        /**
         * 保存交接单
         * 
         * @param call
         */
        *createDeliveryReceipt({ payload },{ call }){
            const res = yield call(createDeliveryReceipt, { ...R.omit(['onSuccess', 'onFail'])(payload) });
            if(res && res.data.resultCode === "ok"){
                message.info("保存成功！");
                if(payload.onSuccess) payload.onSuccess();
            } else {
                if(payload.onFail) payload.onFail();
            }
        },
        /**
         * 修改交接单
         *
         * @param call
         * @param put
         * @param select
         */
        *updateDeliveryReceipt({ payload },{ call, put }){
            const res = yield call(updateDeliveryReceipt, { ...R.omit(['onSuccess', 'onFail'])(payload) });
            if(res && res.data.resultCode === "ok"){
                message.info("修改成功！");
                if(payload.onSuccess) payload.onSuccess(res.data.content);
            } else {
                if(payload.onFail) payload.onFail();
            }
        },
        /**
         * 市场审核交接单
         *
         * @param call
         * @param put
         * @param select
         */
        *ApprovalDeliveryReceipt({ payload },{ call, put }){
            const res = yield call(ApprovalDeliveryReceipt, { ...R.omit(['onSuccess', 'onFail'])(payload) });
            if(res && res.data.resultCode === "ok"){
                message.info("交接单更新成功！");
                if(payload.onSuccess) payload.onSuccess(res.data.content);
            } else {
                if(payload.onFail) payload.onFail();
            }
        },
        /**
         * 删除交接单
         * 
         * @param id
         */
        *deleteById({ payload },{ call }){
            const res = yield call(deleteById,{ ...R.omit(['onSuccess', 'onFail'])(payload) });
            if(res && res.data.resultCode === "ok"){
                message.info("删除成功！");
                if (payload.onSuccess) payload.onSuccess();
            } else {
                if(payload.onFail) payload.onFail();
            }
        },
        /**
         * 根据status获取当前用户的交接单
         *
         * @param status
         * @return
         */
        *getUserDeliveryReceipt({ payload }, { call, put }){
            const res = yield call(getUserDeliveryReceipt, { ...R.omit(['onSuccess', 'onFail'])(payload) });
            if(res && res.data.resultCode === "ok"){
                yield put({
                    type:'refreshState',
                    payload: {
                        flag: res.data.content
                    }
                });
            } else {
                if(payload.onFail) payload.onFail();
            }
        },
        /**
         * 复制交接单
         *
         * @param id
         * @return
         */
        *copyDeliveryReceipt({ payload }, { call, put }){
            const res = yield call(copyDeliveryReceipt, { ...R.omit(['onSuccess', 'onFail'])(payload) });
            if(res && res.data.resultCode === "ok"){
                yield put({
                    type:'refreshState',
                    payload: {
                        dataSource: res.data.content
                    }
                });
                if (payload.onSuccess) payload.onSuccess();
            } else {
                if(payload.onFail) payload.onFail();
            }
        },
    },
    subscriptions: {
        setup({ history, dispatch }) {
            return history.listen(({ pathname }) => {
                const menus = JSON.parse(sessionStorage.getItem(Constants.USER_MENU));
                if(menus && !menus.find(item=>item.includes(pathname)) && pathname != '/logout'){
                    document.location.href = "/notAuthorized"
                }
            });
        }
    }
};